import wx
import wx.lib.agw.aquabutton as AB

from wx.lib.newevent import NewEvent
(ChangeTab, EVT_CHANGE_TAB) = NewEvent()

app_path = "D:\\Eigene Datein\\Python\\TIGS DB\\"
source_path = app_path + "files\\"

class SqlRequest(wx.Panel):
    def __init__(self, parent):
        """Request Panel."""
        wx.Panel.__init__(self, parent, -1)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer = wx.GridBagSizer(hgap=5, vgap=5)

        # Add request widgets
        lables = ["pIPKTA ID", "Construct function", "Gus cells", "Haustoria",
                  "Experiment name", "Cultivar name", "Hours after inoculation",
                  "Pathogen name", "Link to excel sheet"]

        sampleList = ["Ignore", "Equals", "Begins with", "Contains"]

        row = 0
        for lable in lables:
            self.make_widgets(lable, sampleList, row, id=row)
            row += 1

        # Add request limit and button
        text = wx.StaticText(self, -1, "Limit", (8, 10))
        ctrl = wx.TextCtrl(self, -1, "1000", size=(100, -1))
        button = AB.AquaButton(self, -1,None, "Start Request", size=(120,50))

        self.Bind(wx.EVT_BUTTON, self.change_tab_event)

        text.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
        ctrl.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
        button.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))

        self.sizer.Add(text, pos=(9, 1))
        self.sizer.Add(ctrl, pos=(9, 2))
        self.sizer.Add(button, pos=(11, 2))

        mainSizer.Add(self.sizer, 0, wx.ALL, 10)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self)

    def make_widgets(self, lable, sampleList, row, id):
        """Function to create text, cb and ctrl widgets."""

        # Widgets
        text = wx.StaticText(self, id+100, lable, (8, 10))
        cb = wx.ComboBox(self, id, "Ignore", (90, 50),
                         (100, -1), sampleList,
                         wx.CB_DROPDOWN|wx.CB_READONLY)
        ctrl = wx.TextCtrl(self, id+10, "", size=(300, -1))

        # Fonts
        text.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
        cb.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
        ctrl.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))

        # Sizers
        self.sizer.Add(text, pos=(row, 1))
        self.sizer.Add(cb, pos=(row, 2))
        self.sizer.Add(ctrl, pos=(row, 3))

    def change_tab_event(self, evt):
        """TIGS Event."""
        self.get_selections()
        # Event-Instanz erstellen
        evt = ChangeTab()
        # Event starten
        self.ProcessEvent(evt)

    def get_selections(self):
        """Get widgets labels and values."""
        # 0-8 = combobox
        # 10-18 = textctrl
        # 100+108 = text

        f_sql = open(source_path + 'sql_query.txt', 'w')

        for i in range(0,9):
            combo = self.FindWindowById(i)
            val_combo = combo.GetValue()
            if val_combo != 'Ignore':
                ctrl = self.FindWindowById(i+10)
                val_ctrl = ctrl.GetValue()
                text = self.FindWindowById(i+100)
                val_text = text.GetLabel()
                f_sql.write(val_text + '\t' + val_combo + '\t' + val_ctrl + '\n')
        f_sql.close()
